CXX = g++
HIPCC = hipcc
HOST_CXXFLAGS = -std=c++17 -O3 -Wall -Wextra -pthread -march=native -mtune=native -funroll-loops -ffast-math -D__HIP_PLATFORM_AMD__ -I${ROCM_PATH}/include -I/opt/hpc/software/mathlib/blas/hygon/0831/include/blis -fPIC
DEVICE_CXXFLAGS = -std=c++17 -I${ROCM_PATH}/include -fPIC
HIPFLAGS = -D__HIP_CLANG_ONLY__ -D__HIP_ROCclr__ -Wno-ignored-attributes -I${ROCM_PATH}/include
LINK_LIBS = -lhipblas -lrocblas -lamdhip64 -lblis
ROCMLIBS = -L/opt/hpc/software/mathlib/blas/hygon/0831/lib -L${ROCM_PATH}/lib -Wl,-rpath=/opt/hpc/software/mathlib/blas/hygon/0831/lib -Wl,-rpath=${ROCM_PATH}/lib

TARGET = matmul
HOST_SRCS = main.cpp matrix.cpp cpu_kernel.cpp utils.cpp
DEVICE_SRCS = dcu_kernel.cpp
HOST_OBJS = $(HOST_SRCS:.cpp=.o)
DEVICE_OBJS = $(DEVICE_SRCS:.cpp=.o)
OBJECTS = $(HOST_OBJS) $(DEVICE_OBJS)

all: $(TARGET)

$(TARGET): $(OBJECTS)
	$(HIPCC) $(DEVICE_CXXFLAGS) --offload-arch=gfx906 $(LINK_LIBS) $(ROCMLIBS) -o $@ $^

$(HOST_OBJS): %.o: %.cpp
	$(CXX) $(HOST_CXXFLAGS) -c $< -o $@

$(DEVICE_OBJS): %.o: %.cpp
	$(HIPCC) $(DEVICE_CXXFLAGS) $(HIPFLAGS) --offload-arch=gfx906 -c $< -o $@

# 依赖关系
main.o: main.cpp matrix.h cpu_kernel.h dcu_kernel.h utils.h
matrix.o: matrix.cpp matrix.h
cpu_kernel.o: cpu_kernel.cpp cpu_kernel.h matrix.h
dcu_kernel.o: dcu_kernel.cpp dcu_kernel.h matrix.h
utils.o: utils.cpp utils.h

# 清理中间文件
clean:
	rm -f $(OBJECTS)

# 清理所有生成文件
distclean: clean
	rm -f $(TARGET)

# 清理所有生成文件和测试结果
cleanall: distclean
	rm -f results/*.txt results/*.csv results/plots/*.png results/nonsquare/*.txt results/nonsquare/*.csv results/nonsquare/plots/*.png results/threadscan/*.txt results/threadscan/*.csv results/threadscan/plots/*.png results/report/*.html
	rm -f performance_*.png
	rm -f input_*.txt
	rm -rf logs
	rm -f *.err

# 重新编译
rebuild: distclean all

.PHONY: all clean distclean cleanall rebuild